package com.hxzy.projectssmplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.projectssmplus.entity.SysArea;
import com.hxzy.projectssmplus.entity.SysMenu;
import com.hxzy.projectssmplus.mapper.SysAreaMapper;
import com.hxzy.projectssmplus.service.SysAreaService;
import com.hxzy.projectssmplus.util.GlobalConst;
import com.hxzy.projectssmplus.vo.AreaTreeVO;
import com.hxzy.projectssmplus.vo.TreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2021/12/07 8:56
 * Description:
 * Version: V1.0
 */
@Service
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea> implements SysAreaService {

    @Autowired
    private SysAreaMapper sysAreaMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<SysArea> getAreaList(Long parentId) {
        Object area = redisTemplate.opsForValue().get(GlobalConst.AREA_REDIS_PREF + parentId);
        if (area != null) {
            List<SysArea> list = (List<SysArea>) area;
            return list;
        }
        QueryWrapper<SysArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        List<SysArea> sysAreaList = this.sysAreaMapper.selectList(queryWrapper);
        redisTemplate.opsForValue().set(GlobalConst.AREA_REDIS_PREF + parentId, sysAreaList);
        return sysAreaList;
    }

    @Override
    public List<AreaTreeVO> getAreaTree() {
        Object areaTree = redisTemplate.opsForValue().get(GlobalConst.AREA_TREE_REDIS_PREF);
        if (areaTree != null) {
            List<AreaTreeVO> areaTreeVOS = (List<AreaTreeVO>) areaTree;
            return areaTreeVOS;
        }
        List<SysArea> allList = this.sysAreaMapper.selectList(new QueryWrapper<>());
        List<AreaTreeVO> buildList = new ArrayList<>();

        allList.stream().filter(p -> p.getParentId() == 0L).forEach(p -> {
            AreaTreeVO parent = new AreaTreeVO();
            parent.setAreaId(p.getAreaId());
            parent.setAreaName(p.getAreaName());

            //判断是否有下级
            getChildTree(allList, parent);

            buildList.add(parent);
        });

        redisTemplate.opsForValue().set(GlobalConst.AREA_TREE_REDIS_PREF,buildList);
        return buildList;
    }

    public void getChildTree(List<SysArea> allList, AreaTreeVO parent) {
        List<AreaTreeVO> buildList = new ArrayList<>();
        allList.stream().filter(p -> p.getParentId() != 0L && p.getParentId() == parent.getAreaId().longValue())
                .forEach(p -> {
                    AreaTreeVO child = new AreaTreeVO();
                    child.setAreaId(p.getAreaId());
                    child.setAreaName(p.getAreaName());

                    getChildTree(allList, child);

                    buildList.add(child);

                    if (buildList.size() > 0) {
                        parent.setChildren(buildList);
                    }
                });
    }


}
